package cn.stylefeng.guns.modular.bms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.bms.entity.City;
import cn.stylefeng.guns.modular.bms.mapper.CityMapper;
import cn.stylefeng.guns.modular.bms.model.params.CityParam;
import cn.stylefeng.guns.modular.bms.model.result.CityResult;
import  cn.stylefeng.guns.modular.bms.service.CityService;
import cn.stylefeng.guns.utils.AsyncCitysUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 城市信息 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-05-24
 */
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements CityService {

    @Override
    public void add(CityParam param){
        City entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CityParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CityParam param){
        City oldEntity = getOldEntity(param);
        City newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void asyncAll() {
           new AsyncCitysUtil().startAsync(this);
    }

    @Override
    public void perfect() {
        //查询二级
        List<CityResult> results=findListBySpec(new CityParam().setLevel(2).setGroupBy(1));
//        填补一级
        results.forEach(item -> {
            //查询上级是否存在
            CityResult result=findBySpec(new CityParam().setLevel(1).setCity(item.getPcity()));
            if (result!=null){
                this.update(new CityParam().setId(result.getId()).setCode(item.getPcode()).setPcode("0"));
            }
        });

        //直辖市市辖区修改为直辖市名称
        List<CityResult> zxs=this.baseMapper.getZhiXiaShiList(new CityParam());
        zxs.forEach(item -> {
            //查询上级是否存在
            CityResult result=findBySpec(new CityParam().setPcode(item.getCode()).setLevel(2));
            if (result!=null){
                this.update(new CityParam().setId(result.getId()).setCity(item.getCity()).setPy(item.getPy()).setIndexLetter(item.getIndexLetter()));
            }
        });
    }

    @Override
    public CityResult findBySpec(CityParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<CityResult> findListBySpec(CityParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public Map<String, Object> findListForApp(CityParam param) {
        String[] alphabet={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        Map<String,Object> result=new HashMap<>();
        result.put("alphabet",alphabet);
        Map<String,Object> citys=new HashMap<>();
        List<Map<String,Object>> cityResults;
        for (String str:alphabet){
            param.setIndexLetter(str);
            cityResults=this.baseMapper.customMapList(param);
            citys.put(str,cityResults);
        }
        result.put("citys",citys);
        List<Map<String,Object>> hot=this.baseMapper.customMapList(new CityParam().setIsHot(1));
        result.put("hotcity",hot);
        //热门城市
        return result;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CityParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CityParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private City getOldEntity(CityParam param) {
        return this.getById(getKey(param));
    }

    private City getEntity(CityParam param) {
        City entity = new City();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
